package cn.hy.preparelesson.common.service;

import cn.hy.preparelesson.common.entity.PlFeedbackLessonDetail;
import cn.hy.preparelesson.common.entity.PlFeedbackLessonDetailExample;
import cn.hy.preparelesson.common.mapper.PlFeedbackLessonDetailMapper;
import cn.hy.preparelesson.common.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 反馈-备课关联 Service
 */
@Service
public class PlFeedBackLessonDetailService {

    @Autowired
    private PlFeedbackLessonDetailMapper plFeedbackLessonDetailMapper;

    /**
     * 查询备课反馈关联集合
     * @param schoolId
     * @param lessonIdList
     * @return
     */
    public List<PlFeedbackLessonDetail> findPFBDListBy(String schoolId, List<String> lessonIdList) {
        PlFeedbackLessonDetailExample example = new PlFeedbackLessonDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andLessonIdIn(lessonIdList);
        return  plFeedbackLessonDetailMapper.selectByExample(example);
    }

    /**
     * 根据schoolId, feedbackId查询备课-反馈关联信息集合
     * @param schoolId
     * @param feedbackId
     * @return
     */
    public List<PlFeedbackLessonDetail> findListBySchoolIdAndFeedbackId(String schoolId, String feedbackId) {
        PlFeedbackLessonDetailExample example = new PlFeedbackLessonDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andFeedbackIdEqualTo(feedbackId);
        return plFeedbackLessonDetailMapper.selectByExample(example);
    }

    /**
     * 根据schoolId, lessonId查询备课-反馈关联信息集合
     * @param schoolId
     * @param lessonId
     * @return
     */
    public List<PlFeedbackLessonDetail> findListBySchoolIdAndLessonId(String schoolId, String lessonId) {
        PlFeedbackLessonDetailExample example = new PlFeedbackLessonDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andLessonIdEqualTo(lessonId);
        return plFeedbackLessonDetailMapper.selectByExample(example);
    }

    /**
     * schoolId feedbackIdList 查询反馈-备课关联关系集合
     * @param schoolId
     * @param feedbackIdList
     * @return
     */
    public List<PlFeedbackLessonDetail> findListBySchoolIdAndFeedbackIdIn(String schoolId, List<String> feedbackIdList) {
        PlFeedbackLessonDetailExample example = new PlFeedbackLessonDetailExample();
        example.createCriteria().andSchoolIdEqualTo(schoolId).andFeedbackIdIn(feedbackIdList);
        return plFeedbackLessonDetailMapper.selectByExample(example);
    }

    /**
     * 批量保存反馈 - 备课 关系关系
     * @param schoolId
     * @param opeUserId
     * @param feedbackId
     * @param lessonIdList
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveFBLD(String schoolId, String opeUserId, String feedbackId, List<String> lessonIdList, Date now) {
        List<PlFeedbackLessonDetail> list = new ArrayList<>();
        for (String lessonId : lessonIdList ) {
            PlFeedbackLessonDetail detail = new PlFeedbackLessonDetail();
            String id = IdUtils.newId();
            detail.setId(id);
            detail.setFeedbackId(feedbackId);
            detail.setLessonId(lessonId);
            detail.setSchoolId(schoolId);
            detail.setCreateAt(now);
            detail.setCreateBy(opeUserId);
            list.add(detail);
        }
        plFeedbackLessonDetailMapper.batchSaveFeedBackDetail(list);
    }



    /***
     * 批量删除备课-反馈关联关系
     * @param schoolId
     * @param feedbackId
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteFBLD(String schoolId, String feedbackId) {
        plFeedbackLessonDetailMapper.deleteBySchoolIdAndFeedBackId(schoolId, feedbackId);
    }

    /**
     * 修改 反馈-备课关联关系
     * @param schoolId
     * @param opeUserId
     * @param feedbackId
     * @param lessonIdList
     * @param now
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateFBLDBy(String schoolId, String opeUserId, String feedbackId, List<String> lessonIdList, Date now) {
        batchDeleteFBLD(schoolId, feedbackId);
        if (!CollectionUtils.isEmpty(lessonIdList)) {
            batchSaveFBLD(schoolId, opeUserId, feedbackId, lessonIdList, now);
        }
    }
}
